﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Net;
using System.Net.Mail;
using Web_HengliSeed.ClassFile;
using System.Configuration;

namespace Web_HengliSeed.BLL
{
    public class GuestBook
    {
        public static bool Insert(string user, string email, string phone, string content,string ip)
        {
            string strSql = string.Format("insert into T_GuestBook(T_Name,T_Email,T_Phone,T_Content,T_IP) values('{0}','{1}','{2}','{3}','{4}')",
                                    user, email, phone, content,ip);

            int res =  ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, strSql, null);

            if (res > 0)
            {
                string receive = ConfigurationManager.AppSettings["ReceiveEmail"];
                if (!string.IsNullOrEmpty(receive))
                {
                    string con = string.Format("<p>{0}发表留言：</p><p>Email：{1}</p><p>Phone：{2}</p><p>Content：{3}</p>", user, email, phone, content);
                    //发送邮件
                    Function.SendMail(receive, con, "网站有新留言！");
                }
            }

            return res > 0;
        }

        public static bool Update(int id, string content)
        {
            string strSql = string.Format("update T_GuestBook set T_AdminContent='{0}' where [T_ID]={1}", content, id);

            return (ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, strSql, null) > 0);
        }

        public static bool Delete(string ids)
        {
            string strSql = string.Format("delete from T_GuestBook where [T_ID] in ({0})", ids);

            return (ClassFile.AccessHelper.ExecuteNonQuery(ClassFile.AccessHelper.ConnectionString, strSql, null) > 0);
        }

        public static DataTable GetList(int PageIndex, int PageSize, out int PageCount, out int RecordCount, string strWhere)
        {
            try
            {
                string strSql = " select T_ID,T_Name,T_Email,T_Phone,T_Content,T_DateTime,T_AdminContent,T_AdminDateTime,T_IP from  T_GuestBook ";
                string strShow = " T_ID,T_Name,T_Email,T_Phone,T_Content,T_DateTime,T_AdminContent,T_AdminDateTime,T_IP ";
                return ClassFile.AccessHelper.ExecutePager(PageIndex, PageSize, "T_ID", strShow, strSql, strWhere, " T_DateTime DESC ", out PageCount, out RecordCount);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    }
}